package com.shop.filter;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;

public class SysFilter  extends StrutsPrepareAndExecuteFilter {
	private String[] array;


@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
		FilterChain arg2) throws IOException, ServletException {
	HttpServletRequest request = (HttpServletRequest) arg0;
	HttpServletResponse response = (HttpServletResponse) arg1;
	String path = request.getServletPath();
	request.setCharacterEncoding("UTF-8");
	HttpSession session = request.getSession();
	
	boolean  ok=false;
	System.out.println(path);
	for (int i = 0; i < array.length; i++) {
	
		if(path.equals(array[i])){
			
			ok=true;
			break;
		}
		
	}
	
	if(ok){
		if(session.getAttribute("user")==null){
			response.sendRedirect("/bookshop/customer/login.jsp");
			return ;
		
		}
	}
	
	super.doFilter(arg0, arg1, arg2);
}
	@Override
	public void init(FilterConfig arg0) throws ServletException {
		
		
		System.out.println("开始生成Filter===================================");
		Properties pro = new Properties();
		try {
			pro.load(this.getClass().getResourceAsStream("/filter.properties"));
			String pages = pro.getProperty("pages");
			array = pages.split(",");

		} catch (IOException e) {
			
			e.printStackTrace();
		}
	
		super.init(arg0);
	}
}
